<?php
header('Content-Type: application/json; charset=utf-8');

// 引入数据库配置
$dbConfig = include('db_config.php');

// 获取前端传过来的文章信息
$article_id = $_POST['article_id'] ?? '';
$article_title = $_POST['article_title'] ?? '';
$article_description = $_POST['article_description'] ?? '';
$article_content = $_POST['article_content'] ?? '';
$article_type = $_POST['article_type'] ?? '';
$article_likes = $_POST['article_likes'] ?? '';
$article_link = $_POST['article_link'] ?? '';
$article_cover = $_POST['article_cover'] ?? '';
$article_comments = $_POST['article_comments'] ?? '';
$article_remarks = $_POST['article_remarks'] ?? '';
$review_status = $_POST['review_status'] ?? '假';
$reviewer_id = $_POST['reviewer_id'] ?? '';
$rejection_reason = $_POST['rejection_reason'] ?? '';

// 检查文章 ID 是否为空
if (empty($article_id)) {
    $result = [
        'status' => 'error',
        'message' => '文章 ID 不能为空'
    ];
    echo json_encode($result, JSON_UNESCAPED_UNICODE);
    exit;
}

// 创建数据库连接
try {
    $dsn = "mysql:host=" . $dbConfig['host'] . ";dbname=" . $dbConfig['dbname'] . ";charset=" . $dbConfig['charset'];
    $pdo = new PDO($dsn, $dbConfig['username'], $dbConfig['password']);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    // 查询文章信息
    $selectArticleStmt = $pdo->prepare("SELECT * FROM mi_bbs WHERE id = :article_id");
    $selectArticleStmt->bindParam(':article_id', $article_id);
    $selectArticleStmt->execute();
    $article = $selectArticleStmt->fetch(PDO::FETCH_ASSOC);

    if (!$article) {
        $result = [
            'status' => 'error',
            'message' => '文章不存在'
        ];
        echo json_encode($result, JSON_UNESCAPED_UNICODE);
        exit;
    }

    // 构建更新语句
    $updateParts = [];
    $params = [];

    if (!empty($article_title)) {
        $updateParts[] = "文章标题 = :article_title";
        $params[':article_title'] = $article_title;
    }
    if (!empty($article_description)) {
        $updateParts[] = "文章介绍 = :article_description";
        $params[':article_description'] = $article_description;
    }
    if (!empty($article_content)) {
        $updateParts[] = "文章内容 = :article_content";
        $params[':article_content'] = $article_content;
    }
    if (!empty($article_type)) {
        $updateParts[] = "文章类型 = :article_type";
        $params[':article_type'] = $article_type;
    }
    if (!empty($article_likes)) {
        $updateParts[] = "获赞数量 = :article_likes";
        $params[':article_likes'] = $article_likes;
    }
    if (!empty($article_link)) {
        $updateParts[] = "附加链接 = :article_link";
        $params[':article_link'] = $article_link;
    }
    if (!empty($article_cover)) {
        $updateParts[] = "文章封面 = :article_cover";
        $params[':article_cover'] = $article_cover;
    }
    if (!empty($article_comments)) {
        $updateParts[] = "文章评论 = :article_comments";
        $params[':article_comments'] = $article_comments;
    }
    if (!empty($article_remarks)) {
        $updateParts[] = "文章备注 = :article_remarks";
        $params[':article_remarks'] = $article_remarks;
    }
    if (!empty($review_status)) {
        $updateParts[] = "是否已审核 = :review_status";
        $params[':review_status'] = $review_status;
    }
    if (!empty($reviewer_id)) {
        $updateParts[] = "审核者的用户id = :reviewer_id";
        $params[':reviewer_id'] = $reviewer_id;
    }
    if (!empty($rejection_reason)) {
        $updateParts[] = "拒绝原因 = :rejection_reason";
        $params[':rejection_reason'] = $rejection_reason;
    }

    if (empty($updateParts)) {
        $result = [
            'status' => 'error',
            'message' => '没有需要更新的内容'
        ];
        echo json_encode($result, JSON_UNESCAPED_UNICODE);
        exit;
    }

    // 构建完整的更新语句
    $updateStmt = "UPDATE mi_bbs SET " . implode(', ', $updateParts) . " WHERE id = :article_id";
    $params[':article_id'] = $article_id;

    // 准备并执行更新语句
    $stmt = $pdo->prepare($updateStmt);
    $stmt->execute($params);

    // 检查更新是否成功
    if ($stmt->rowCount() > 0) {
        $result = [
            'status' => 'success',
            'message' => '文章信息更新成功'
        ];
    } else {
        $result = [
            'status' => 'error',
            'message' => '文章信息更新失败'
        ];
    }

    echo json_encode($result, JSON_UNESCAPED_UNICODE);

} catch (PDOException $e) {
    // 数据库操作失败
    $result = [
        'status' => 'error',
        'message' => '数据库操作失败：' . $e->getMessage()
    ];
    echo json_encode($result, JSON_UNESCAPED_UNICODE);
}
?>